<template>
  <div class="create-post-container">
    <el-form ref="postForm" :model="postForm" label-position="top" label-width="100px" class="bu-form">
      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="城市名称" prop="cityName">
            <el-input v-model="postForm.cityName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="18" :offset="3">
          <el-form-item label="CODE" prop="cityCode">
            <el-input v-model="postForm.cityCode" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="mt30">
        <el-col :span="18" :offset="3">
          <el-form-item>
            <el-button type="primary" @click="submitForm">保 存</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  getCityShow,
  addCity,
  editCity
} from '@/api/cities'

const defaultForm = {
  id: undefined,
  cityName: '',
  cityCode: ''
}

export default {
  name: 'BuSettingForm',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      provinceOptions: [],
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        cityName: [
          { required: true, message: '请输入城市名称', trigger: 'blur' }
        ],
        cityCode: [
          { required: true, message: '请输入CODE', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.params && this.$route.params.id
      this.fetchData(id)
    }
  },
  methods: {
    fetchData(id) {
      getCityShow({ id }).then(response => {
        const { data } = response
        // console.log(data)
        this.postForm.id = data.id
        this.postForm.cityName = data.cityName
        this.postForm.cityCode = data.cityCode
      }).catch(err => {
        console.log(err)
      })
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.isEdit) {
            editCity(this.postForm).then(response => {
              // console.log(response)
              this.$notify({
                title: '成功',
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.$router.go(-1)
            }).catch(err => {
              console.log(err)
            })
          } else {
            addCity(this.postForm).then(response => {
              // console.log(response)
              this.$notify({
                title: '成功',
                message: '添加成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.$router.go(-1)
            }).catch(err => {
              console.log(err)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.postForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.create-post-container {
  position: relative;
  .bu-form {
    padding: 40px;
  }
}
</style>
